package com.itggm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itggm.Result.ResponseResult;
import com.itggm.dto.CoursePageDTO;
import com.itggm.pojo.Course;
import com.itggm.service.CourseService;
import com.itggm.mapper.CourseMapper;
import org.springframework.stereotype.Service;

/**
* @author root
* @description 针对表【sys_course】的数据库操作Service实现
* @createDate 2024-12-27 17:04:31
*/
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course>
    implements CourseService{

    @Override
    public ResponseResult coursePage(CoursePageDTO coursePageDTO) {
        // 分页查询
        Page<Course> coursePage = new Page<>(coursePageDTO.getPage(), coursePageDTO.getLimit());
        // 构建分页条件
        LambdaQueryWrapper<Course> wrapper = new LambdaQueryWrapper<>();
        String courseName = coursePageDTO.getCourseName();
        wrapper.like(StringUtils.isNotBlank(courseName),Course::getCourseName, courseName)
                .orderByDesc(Course::getCreateTime);
        // 执行分页
        baseMapper.selectPage(coursePage, wrapper);
        // 返回分页结果
        return ResponseResult.success()
                .data("total", coursePage.getTotal())
                .data("tableData", coursePage.getRecords());
    }

    @Override
    public ResponseResult updateCourseStatus(Integer id, Integer status) {
        Course course = new Course();
        course.setId(id);
        course.setIsDelete(status);
        baseMapper.updateById(course);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult deleteCourse(Integer id) {
        LambdaUpdateWrapper<Course> lambda = new UpdateWrapper<Course>().lambda();
        lambda.set(Course::getIsDelete,1)
                .eq(Course::getId,id);
        baseMapper.update(lambda);

        return ResponseResult.success();
    }
}




